import { GatherMachineAdjustmentService } from './machineAdjustment.service';
import { Body, Controller, Post, Get, Req } from '@nestjs/common';
import { ApiOkResponse, ApiOperation, ApiSecurity, ApiTags } from '@nestjs/swagger';
import { ADMIN_PREFIX } from '../../admin.constants';
import { AdminUser } from '../../core/decorators/admin-user.decorator';
import { FastifyRequest } from 'fastify';

@ApiSecurity(ADMIN_PREFIX)
@ApiTags('设备调机配置管理模块')
@Controller('machineAdjustment')
export class GatherMachineAdjustmentController {
  constructor(private gatherMachineAdjustmentService: GatherMachineAdjustmentService) {}

  @ApiOperation({ summary: '获取设备调机配置列表' })
  @ApiOkResponse()
  @Get('list')
  async list() {
    return await this.gatherMachineAdjustmentService.list();
  }

  @ApiOperation({ summary: '分页获取获取设备调机配置列表' })
  @ApiOkResponse()
  @Post('page')
  async page(@Body() dto) {
    const [list, total] = await this.gatherMachineAdjustmentService.page(dto);

    return {
      list,
      pagination: {
        total,
        pageNum: dto.pageNum,
        pageSize: dto.pageSize,
      },
    };
  }

  @ApiOperation({ summary: '添加设备调机配置' })
  @ApiOkResponse()
  @Post('add')
  async add(@Body() dto, @AdminUser() user, @Req() req: FastifyRequest) {
    const { mac = '', ip = '', synchostsqlite: syncHostSqlite = '' } = req.headers;

    await this.gatherMachineAdjustmentService.isExistKey(dto);

    await this.gatherMachineAdjustmentService.add(dto, user, mac, ip, syncHostSqlite);
  }

  @ApiOperation({ summary: '修改设备调机配置' })
  @ApiOkResponse()
  @Post('update')
  async update(@Body() dto, @AdminUser() user, @Req() req: FastifyRequest) {
    const { mac = '', ip = '', synchostsqlite: syncHostSqlite = '' } = req.headers;

    await this.gatherMachineAdjustmentService.update(dto, user, mac, ip, syncHostSqlite);
  }

  @ApiOperation({ summary: '删除设备调机配置' })
  @ApiOkResponse()
  @Post('delete')
  async delete(@Body() dto, @AdminUser() user, @Req() req: FastifyRequest) {
    const { mac = '', ip = '', synchostsqlite: syncHostSqlite = '' } = req.headers;

    await this.gatherMachineAdjustmentService.delete(dto, user, mac, ip, syncHostSqlite);
  }

  @ApiOperation({ summary: '获取设备调机详情' })
  @ApiOkResponse()
  @Post('info')
  async info(@Body() dto) {
    return await this.gatherMachineAdjustmentService.info(dto);
  }
}
